import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/home/HomeView.vue'
import LoginView from '../views/login/LoginView.vue'
import OverView from '../views/home/components/mainComponents/OverView.vue'
import MainRegister from '../views/home/components/mainComponents/MainRegister.vue'
import MainManager from '../views/home/components/mainComponents/MainManager.vue'
import DataDictionary from '../views/home/components/mainComponents/DataDictionary.vue'
import DeviceDetail from '../views/home/components/mainComponents/registerComponents/DeviceDetail.vue'
import DeviceEdit from '../views/home/components/mainComponents/registerComponents/DeviceEdit.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView,
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta: {
      login_require: true
    },
    children: [
      {
        path: '/home',
        redirect: '/home/overview'
      },
      {
        path: "overview",
        name: "overview",
        component: OverView
      },
      {
        path: "register",
        name: "register",
        component: MainRegister,
        children:[
          {
            path:'detail',
            name:'detail',
            component: DeviceDetail
          },
          {
            path:'edit',
            name:'edit',
            component: DeviceEdit
          }
        ]
      },
      {
        path: "manager",
        name: "manager",
        component: MainManager
      },
      {
        path: "dictionary",
        name: "dictionary",
        component: DataDictionary
      }

    ]
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  var flag = to.matched.some(item => item.meta.login_require)
  if (flag) {
    if (localStorage.getItem('token')) {
      next()
    } else {
      alert('未获取您的登录信息，即将前往登录页')
      next('/')
    }
  } else {
    next()
  }

  // if(to.name == 'login') {
  //   next()
  // }else {
  //   if(localStorage.getItem('token')){
  //     next()
  //   }else {
  //     alert('未获取您的登录信息，即将跳往登录页')
  //     next('/')
  //   }
  // }
})

export default router
